﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBNCSUtil;

namespace NyxBot
{
    class clsBNCS
    {
        // Class values
        private bool veto = false;

        // Premade classes
        public clsConfig config;
        public clsSocket socket;

        // Events
        // BNCS-protocol packets
        public delegate void BNCSEventHandler(object sender, byte id, short length, byte[] data);
        // Socket events go here
        public event BNCSEventHandler OnSendProtocolID;
        public event BNCSEventHandler OnSendingPacket;
        public event BNCSEventHandler OnSendPacket;
        public event BNCSEventHandler OnRecvPacket;
        public delegate void ChatEventHandler(object sender, string username, int flags, string text, int ping);

        public clsBNCS(clsConfig configuration)
        {
            // Load up the configuration file for whatever is controlling this
            config = configuration;

            // Replace these with config entries later
            socket = new clsSocket("uswest.battle.net", 6112, true);

            // ...
        }

        public bool Connect()
        {
            bool status = false;
            status = socket.Connect();
            // Send 0x01 if successful.
            if (status)
                SendProtocol();
            return status;
        }

        public bool Disconnect()
        {
            bool status = false;
            status = socket.Disconnect();
            return status;
        }

        private bool SendData(byte[] data)
        {
            return socket.SendData(data);
        }

        // Everything below is related to BNCS

        public void SendPacket(byte id, byte[] data)
        {
            veto = false;
            short length = 0;

            // ...

            if (OnSendingPacket != null)
                OnSendingPacket(this, id, length, data);

            if (!veto)
            {
                // ...
            }
        }

        public void VetoPacket()
        {
            veto = true;
        }

        private void SendProtocol()
        {
            byte proto = 0x01;
            byte[] data = new byte[0];
            data[0] = proto;
            socket.SendData(data);
        }
    }
}
